package gdut.jmxywz.action;

import gdut.jmxywz.been.Info;
import gdut.jmxywz.been.InfoFiles;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.ServletActionContext;



@SuppressWarnings("serial")
public class ScientificInfoFilesAction extends BaseAction {
	
	private String  infoId;
	private String  infoFilesId;
	private String  fileName;
	private String  fileContext;
	private String  fileUrl;
	private List<InfoFiles>  files;
	private InfoFiles infoFiles;
	
	
	public String getInfoId() {
		return infoId;
	}
	public void setInfoId(String infoId) {
		this.infoId = infoId;
	}
	public String getInfoFilesId() {
		return infoFilesId;
	}
	public void setInfoFilesId(String infoFilesId) {
		this.infoFilesId = infoFilesId;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileContext() {
		return fileContext;
	}
	public void setFileContext(String fileContext) {
		this.fileContext = fileContext;
	}
	public String getFileUrl() {
		return fileUrl;
	}
	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	}
	public List<InfoFiles> getFiles() {
		return files;
	}
	public void setFiles(List<InfoFiles> files) {
		this.files = files;
	}
	
	public InfoFiles getInfoFiles() {
		return infoFiles;
	}
	public void setInfoFiles(InfoFiles infoFiles) {
		this.infoFiles = infoFiles;
	}
	/***
	 * Delete 删除附件
	 */
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	public String  Delete() throws Exception{
		
 
        int id = Integer.parseInt(infoId);
        int filesId=0;
     
//        boolean b = this.getCommonService().deleteAllFile(id,filesId);
         boolean b = false;
         
         String hql = "from InfoFiles inf where inf.info.infoId="+id;
         System.out.println("=+++++++ "+infoFilesId);
         if(!"".equals(infoFilesId)&&infoFilesId!=null){
         	filesId = Integer.parseInt(infoFilesId);
         	
         	hql = hql + " and infoFilesId = "+filesId;
         }
         String []parem = null;
         
         System.out.println("-------hql------"+hql);
         List i = this.getCommonService().findListByHql(hql, parem);
         
         Iterator iter = i.iterator();
         
         while(iter.hasNext()){
        	 
        	 InfoFiles infofiles = (InfoFiles)iter.next();
        	 
        	 System.out.println("++准备删除文件++");
        	 b=this.getCommonService().deleteObject(infofiles);
        	 
        
        	 System.out.println("-------------删除成功--------------");
        	 
	         String path =ServletActionContext.getRequest().getRealPath("/");
	        
	         System.out.println("----------------"+path);
	         
	         path =path+infofiles.getFileUrl();
	         
	         System.out.println("++++"+path);
	         
	         boolean b1 = this.deleteFile(path);//输入路径，调用deletefile（）将文件从服务器中删除
	         if(b1){
	        		System.out.println("---从服务器上文件删除成功！");
	        		
	        	}else{
	        		System.out.println("----从服务器上文件删除失败！");
	        		
	        	}   	 
        	 
        	 
         }
         
         
		if(b){
			return "ScientificInfoFilesDelete";
		}
		else{
			return "error";
		}
         
	}
	
	/***
	 * preUpdate 显示将要删除附件的信息
	 */
	
	public  String PreUpdate() throws Exception{
		 
			int  id  = 0 ;
			
			if(!"".equals(infoFilesId)&&infoFilesId!=null){
				
				id = Integer.parseInt(infoFilesId);
			}
			
			infoFiles =(InfoFiles) this.getCommonService().findByIntId(id, "gdut.jmxywz.been.InfoFiles");
			
			System.out.println("++++++++++++++++++++++++++++++++++");
			
			System.out.println("========="+infoFiles.getFileName());
			
			Info info = new Info();
			  info =infoFiles.getInfo();
			
			System.out.println("-------rrr--------"+info.getInfoTitle());
			
		
		return "ScientificInfoFilesPreUpdate";
	}
	
	
	
	
	
	/***
	 * 
	 * update 修改部件
	 */
	
	
	public String  Update() throws Exception{
		
		int  fileid  = 0 ;
		int  infId = 0; 
		
		InfoFiles infoFiles = new  InfoFiles();
		
		if(!"".equals(infoFilesId)&&infoFilesId!=null&&!"".equals(infoId)&&infoId!=null){
			
			fileid = Integer.parseInt(infoFilesId);
			
			infId = Integer.parseInt(infoId);
			
			Info info  = (Info)this.getCommonService().findByIntId(infId, "gdut.jmxywz.been.Info");
			
			infoFiles.setInfoFilesId(fileid);
			infoFiles.setFileContext(fileContext);
		    infoFiles.setFileName(fileName);
		    infoFiles.setFileUrl(fileUrl);
		    
		    infoFiles.setInfo(info);
		}
		
	    boolean b= this.getCommonService().updateObject(infoFiles);
		
	    if(b){
	    	
	    	System.out.println("附件信息修改成功！");
	    	
	    	return "ScientificInfoFilesUpdate";
	    }else{
	    	
	    	System.out.println("附件信息修改失败！");
	    	
	    	return "error";
	    }
		
	}

	
	public  boolean DeleteFiles(String sPath) {   
		boolean  flag = false;   
		File   file = new File(sPath);   
	    // 路径为文件且不为空则进行删除   
	    if (file.isFile() && file.exists()) {   
	        file.delete();   
	        System.out.println("---成功从服务器删除文件---");
	        flag = true;   
	    }   
	    return flag;   
	}  
	
	
	
	
	   /**
	    *  deleteFile  从服务器上删除附件
	    * @param sPath
	    * @return
	    */
		
		public  boolean deleteFile(String sPath) {   
			boolean  flag = false;   
			File   file = new File(sPath);   
		    // 路径为文件且不为空则进行删除   
		    if (file.isFile() && file.exists()) {   
		        file.delete();   
		        System.out.println("---成功从服务器删除文件---");
		        flag = true;   
		    }   
		    return flag;   
		}  

}
